<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Input;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\RecommendPosition;
use App\Models\Recommend;
use App\Http\Requests\Admin\RecommendPosition\StoreRequest;
use App\Http\Requests\Admin\RecommendPosition\UpdateRequest;
use Gate;
use App\Exceptions\Admin\PermissionDeniedException;

class RecommendPositionController extends Controller
{
    public function recommend_position_gate()
    {
        if (!Gate::allows('goods','')) {
            throw new PermissionDeniedException();
        }//end if()

    }//end func __construce()

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $this->recommend_position_gate();

        $search = array('name'=>'');
        $recommendPosition = RecommendPosition::paginate(10);

        if (!empty($request->get('name'))){
            $recommendPosition = RecommendPosition::where('name', 'like', "%".trim($request->get('name'))."%")->paginate(10);
            $search['name'] = $request->name;
        }

        return view('admin.recommend_position.index',compact('recommendPosition','search'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(RecommendPosition $recommendPosition)
    {
        $this->recommend_position_gate();

        return view('admin.recommend_position.create',compact('recommendPosition'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreRequest $request , RecommendPosition $recommendPosition)
    {
        $this->recommend_position_gate();

        $param = $request->all();

        $recommendPosition->create($param);
        return Redirect('admin/recommend_position');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $this->recommend_position_gate();

        $recommendPosition = RecommendPosition::find($id);
        return view('admin.recommend_position.edit',compact('recommendPosition'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateRequest $request, $id)
    {
        $this->recommend_position_gate();

        $recommendPosition = RecommendPosition::find($id);

        $recommendPosition->update($request->all());

        return Redirect('admin/recommend_position');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $this->recommend_position_gate();

        $recommendPosition = RecommendPosition::find($id);
        $recommend = Recommend::where('recommend_position_id',$id);

        $recommend->delete();

        $recommendPosition->delete();



        return Redirect('admin/recommend_position');
    }

    public function toggleShow(){

        $this->recommend_position_gate();

        $id = Input::get("id");
        $is_recommend = RecommendPosition::where("id",$id)->pluck("is_recommend");
        $data = array();

        if($is_recommend==0){
            $data['is_recommend'] = 1;
            $data['id'] = $id;
            $res = RecommendPosition::where("id",$id)->update(['is_recommend'=>1]);
        }else{
            $data['is_recommend'] = 0;
            $data['id'] = $id;
            $res = RecommendPosition::where("id",$id)->update(['is_recommend'=>0]);
        }
        if($res != false){
            $data['info'] = true;
        }else{
            $data['info'] = false;
        }
        return $data;

    }
}
